/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.service.data.entitydiff;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import io.swagger.annotations.ApiModelProperty;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;


/**
 * data-entityDiff数据对比工具的默认实现
 *
 * @see DiffActor
 * @author yoara
 */
public class DefaultDiffActor implements DiffActor{
    @Override
    public void doAct(DiffParam param,DiffResult result) {
        List<Field> diffFieldList = param.pickDiffFieldList();
        Map<String,Field> fieldMap = param.pickEntityFieldMap(diffFieldList);
        StringBuffer content = new StringBuffer();
        try{
            for(Field field:diffFieldList){
                Object historyValue = field.get(param.getHistoryEntity());
                Object nowValue = fieldMap.get(field.getName())==null
                        ?null:fieldMap.get(field.getName()).get(param.getEntity());
                ApiModelProperty apiDesc = field.getAnnotation(ApiModelProperty.class);

                String hvStr = historyValue instanceof String?(String)historyValue:
                        JSONObject.toJSONString(historyValue,features);
                String nvStr = nowValue instanceof String?(String)nowValue:
                        JSONObject.toJSONString(nowValue,features);
                if(!hvStr.equals(nvStr)){
                    content.append("├─").append("[").append(apiDesc==null?field.getName():apiDesc.value()).append("]")
                            .append("(").append(hvStr).append(" => ").append(nvStr)
                            .append(")").append("\r\n");
                }
            }
        }catch (Exception e){
            result.setErrMessage(e.getMessage());
        }
        result.setResult(true);
        result.setData(content.toString());
    }

    public static SerializerFeature[] features = {
            SerializerFeature.WriteMapNullValue,//输出值为null的字段
            SerializerFeature.WriteNullNumberAsZero, //数值字段如果为null,输出为0
            SerializerFeature.WriteNullStringAsEmpty, //字符类型字段如果为null,输出为""
            SerializerFeature.WriteNullListAsEmpty,//List字段如果为null,输出为[]
            SerializerFeature.WriteNullBooleanAsFalse,//Boolean字段如果为null,输出为false,而非null
            SerializerFeature.DisableCircularReferenceDetect,//禁止循环引用检测
            SerializerFeature.WriteDateUseDateFormat//使用格式化日期
    };
}
